package com.xlhj.jdbc.dao;

import com.xlhj.jdbc.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @ClassName SysUserDao
 * @Description 用户表持久层
 * @Author liucaijing
 * @Date 2021/2/24 11:16
 * @Version 1.0
 */
@Repository
public class SysUserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询全部用户信息
     * @return
     */
    public List<SysUser> selectAllUser() {
        List<SysUser> userList = jdbcTemplate.query("select * from sys_user", new BeanPropertyRowMapper<>(SysUser.class));
        return userList;
    }

    /**
     * 根据主键查询用户信息
     * @param id
     * @return
     */
    public SysUser selectUserById(Long id) {
        SysUser user = jdbcTemplate.queryForObject("select * from sys_user where id = ?", new BeanPropertyRowMapper<>(SysUser.class), id);
        return user;
    }

    /**
     * 新增用户信息
     * @param user
     * @return
     */
    public int insertUser(SysUser user) {
        int count = jdbcTemplate.update("insert into sys_user(user_code, user_name, password, sex, age, email) values (?, ?, ?, ?, ?, ?)", user.getUserCode(), user.getUserName(), user.getPassword(), user.getSex(), user.getAge(), user.getEmail());
        return count;
    }

    /**
     * 根据主键ID更新用户信息
     * @param user
     * @return
     */
    public int updateUser(SysUser user) {
        int count = jdbcTemplate.update("update sys_user set user_code = ?, user_name = ?, password = ?, sex = ?, age = ?, email = ? where id = ?", user.getUserCode(), user.getUserName(), user.getPassword(), user.getSex(), user.getAge(), user.getEmail(), user.getId());
        return count;
    }

    /**
     * 根据主键ID删除用户信息
     * @param id
     * @return
     */
    public int deleteUserById(Long id) {
        int count = jdbcTemplate.update("delete from sys_user where id = ?", id);
        return count;
    }
}
